import { defineStore } from 'pinia'
import { getCurrentUser } from '@/api/auth'

export const useUserStore = defineStore('user', {
    state: () => ({
        user: null
    }),
    actions: {
        // 获取用户信息
        async fetchUserInfo() {
            try {
                this.user = await getCurrentUser()
            } catch (error) {
                this.user = null
                throw error
            }
        },
        // 清除用户信息
        clearUser() {
            this.user = null
        }
    },
    getters: {
        // 判断是否已登录
        isLoggedIn: state => !!state.user
    }
})